%{?!tool_name: %global tool_name hosttools}
%global tool_prefix /opt/%{tool_name}

%bcond_without bootstrap
%bcond_with zstd

# build procedure: binutils -> bootstrap gcc -> binutils zstd -> gcc with zstd

%global gmp_version 6.1.2
%global mpfr_version 4.1.0
%global mpc_version 1.2.1
%global isl_version 0.24
%global gcc_version 11.3.0

Name: %{tool_name}-gcc
Version: 11.3.0
Release: 1%{?dist}
Summary: GNU compiler collection
Provides: cpp = %{version}-%{release}
Provides: gcc = %{version}-%{release}
Provides: gcc-c++ = %{version}-%{release}
Provides: gcc-gnat = %{version}-%{release}

License: GPLv3+
URL: https://gcc.gnu.org
Source0: https://gcc.gnu.org/pub/gcc/releases/gcc-11.3.0/gcc-11.3.0.tar.xz
Source1: https://gmplib.org/download/gmp/gmp-%{gmp_version}.tar.lz
Source2: https://ftp.gnu.org/gnu/mpfr/mpfr-%{mpfr_version}.tar.xz
Source3: https://ftp.gnu.org/gnu/mpc/mpc-%{mpc_version}.tar.gz
Source4: https://gcc.gnu.org/pub/gcc/infrastructure/isl-%{isl_version}.tar.bz2
Patch0: gcc-static-libsanitizer.patch

BuildRequires: gcc-c++ gcc-gnat lzip texinfo
%if %{with zstd}
BuildRequires: zstd
%endif

%description
GNU compiler collection

%prep
%setup -q -n gcc-%{gcc_version} -a 1 -a 2 -a 3 -a 4
mv gmp-%{gmp_version} gmp
mv mpfr-%{mpfr_version} mpfr
mv mpc-%{mpc_version} mpc
mv isl-%{isl_version} isl
%patch0 -p1

#  --with-build-config=bootstrap-lto \
%build
mkdir build-gcc
cd build-gcc
CFLAGS=-fPIC CXXFLAGS=-fPIC ../configure --prefix=%{tool_prefix} \
  --disable-multilib \
%if %{with bootstrap}
  --enable-bootstrap \
%else
  --disable-bootstrap \
%endif
  --with-linker-hash-style=gnu \
  --enable-languages=c,c++,ada,lto \
  --enable-lto \
  --enable-plugin \
  --disable-shared \
  --enable-threads=posix \
  --enable-__cxa_atexit \
  --enable-cet=auto \
  --enable-clocale=gnu \
  --enable-gnu-indirect-function \
  --enable-gnu-unique-object \
  --enable-linker-build-id \
  --disable-libssp \
  --disable-libstdcxx-pch \
  --disable-werror \
  --disable-nls \
  --enable-link-serialization=1
make %{?_smp_mflags}


%install
cd build-gcc
make DESTDIR=%{buildroot} install
rm -f %{buildroot}%{tool_prefix}/share/info/dir
rm -rf %{buildroot}%{tool_prefix}/share/man/man7
find %{buildroot} -name '*.la' -delete
ln -sf gcc %{buildroot}%{tool_prefix}/bin/cc

cat > %{buildroot}%{tool_prefix}/bin/c89 <<"EOF"
#!/bin/sh
fl="-std=c89"
for opt; do
  case "$opt" in
    -ansi|-std=c89|-std=iso9899:1990) fl="";;
    -std=*) echo "`basename $0` called with non ANSI/ISO C option $opt" >&2
	    exit 1;;
  esac
done
exec %{tool_prefix}/bin/gcc $fl ${1+"$@"}
EOF
cat > %{buildroot}%{tool_prefix}/bin/c99 <<"EOF"
#!/bin/sh
fl="-std=c99"
for opt; do
  case "$opt" in
    -std=c99|-std=iso9899:1999) fl="";;
    -std=*) echo "`basename $0` called with non ISO C99 option $opt" >&2
	    exit 1;;
  esac
done
exec %{tool_prefix}/bin/gcc $fl ${1+"$@"}
EOF
chmod 755 %{buildroot}%{tool_prefix}/bin/c89
chmod 755 %{buildroot}%{tool_prefix}/bin/c99

%files
%{tool_prefix}/bin/*
%{tool_prefix}/lib64/*
%{tool_prefix}/lib/gcc/x86_64-pc-linux-gnu/%{version}
%{tool_prefix}/libexec/gcc/x86_64-pc-linux-gnu/%{version}
%{tool_prefix}/include/c++/%{version}
%{tool_prefix}/share/gcc-%{version}/python/libstdcxx
%{tool_prefix}/share/info/*.info*
%{tool_prefix}/share/man/man1/*

%changelog

